package cao.oschina.bean;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

/**
 * 博客
 * <p/>
 * Created by Haidy on 15/3/28.
 */
public class Blog {

    public String id;
    public String title;
    public String body;
    public String url;
    public String pubDate;
    public int authoruid;
    public String authorname;
    public int commentCount;
    public int documentType;

    public static List<Blog> parser(String xml) {
        List<Blog> blogs = new ArrayList<>();
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
            Element root = document.getDocumentElement();
            NodeList newsList = root.getElementsByTagName("blog");
            for (int i = 0; i < newsList.getLength(); i++) {
                Blog blog = new Blog();
                NodeList childNodes = newsList.item(i).getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
                        Node childNode = childNodes.item(j);
                        switch (childNode.getNodeName()) {
                            case "id":
                                blog.id = childNode.getFirstChild().getNodeValue();
                                break;
                            case "title":
                                blog.title = childNode.getFirstChild().getNodeValue();
                                break;
                            case "body":
                                blog.body = childNode.getFirstChild().getNodeValue();
                                break;
                            case "commentCount":
                                String commentCount = childNode.getFirstChild().getNodeValue();
                                if (commentCount == null) {
                                    blog.commentCount = 0;
                                } else {
                                    blog.commentCount = Integer.parseInt(commentCount);
                                }
                                break;
                            case "pubDate":
                                blog.pubDate = childNode.getFirstChild().getNodeValue();
                                break;
                            case "url":
                                if (childNode.getFirstChild() != null) {
                                    blog.url = childNode.getFirstChild().getNodeValue();
                                }
                                break;
                            case "documentType":
                                String documentType = childNode.getFirstChild().getNodeValue();
                                if (documentType == null) {
                                    blog.documentType = 0;
                                } else {
                                    blog.documentType = Integer.parseInt(documentType);
                                }
                                break;
                            case "authoruid":
                                String authoruid = childNode.getFirstChild().getNodeValue();
                                if (authoruid == null) {
                                    blog.authoruid = 0;
                                } else {
                                    blog.authoruid = Integer.parseInt(authoruid);
                                }
                                break;
                            case "authorname":
                                blog.authorname = childNode.getFirstChild().getNodeValue();
                                break;
                        }
                    }
                }
                blogs.add(blog);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return blogs;
    }
}
